<?php
namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
use app\common\service\Http as HttpService;
use app\common\service\PinYin as PinYinService;

class Forum extends Model
{
	use SoftDelete;
	protected $defaultSoftDelete = 0;

	public function scopeNwhere($query,$param)
	{
		if(!empty($param['cate_id'])){
			$where[] = ['cate_id', '=', $param['cate_id']];
		}
		if(!empty($param['user_id'])){
			$where[] = ['user_id', '=', $param['user_id']];
		}
		if(!empty($param['keyword'])){
			$where[] = ['title', 'like', $param['keyword'].'%'];
		}
		if(!empty($param['top'])){
			$where[] = ['top', '=', $param['top']];
		}
		if(!empty($param['best'])){
			$where[] = ['best', '=', $param['best']];
		}
		if(isset($where)){
			$query->where($where);
		}
	}

	public function scopeNorder($query,$sort){
		if($sort == 1){
			$query->order('top desc,id desc');
		}else if($sort == 2){
			$query->order('top desc,reply_count desc');
		}else if($sort == 3){
			$query->order('top desc,hits desc');
		}else if($sort == 4){
			$query->where('best',1)->order('reply_time desc');
		}else{
			$query->order('top desc,reply_time desc');
		}
	}

	public function getDescAttr($value,$data)
	{
		$content = strip_tags($data['content']);
		$content = str_replace(['	',PHP_EOL,' ','　','&nbsp;'],'',$content);
		return mb_substr($content, 0,90);
	}

	public function attachments()
	{
		return $this->hasMany('attachment');
	}

	public function replys()
	{
		return $this->hasMany('reply');
	}
	
	public function cate()
	{
		return $this->belongsTo('cate');
	}

	public function user()
	{
		return $this->belongsTo('user');
	}
}